package br.ucb.GtTunningLog.client;


import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;


public class BlogEntry extends Widget {
	private Button saveEntryButton;
	private Button cancelButton;
	private Button viewPost;
	private TextArea initBlogText;
	private TextArea fullBlogText;
	private FlowPanel panel;
	private String pageHead;
	private DialogBox dialogBox;
	private HTML serverResponseLabel;
	
//	private GreetingServiceAsync	postService	= GWT
//	.create(GreetingService.class);	
	
	private GreetingServiceAsync	postService;
	
	private static final String			SERVER_ERROR	= "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";

	
	private BlogEntry(){
		init();
	}
	
	private BlogEntry(GreetingServiceAsync postService){
		this.postService = postService;
	}
	
	private Widget getPanel(){
		return this.panel;
	}
	
	private void init(){
		panel = new FlowPanel();
		//initLabel = new Label("Descreva aqui as informações gerais do post");
		pageHead = "Descreva aqui o seu post: ";
		saveEntryButton = new Button("Salvar Post");
		cancelButton = new Button("Cancelar Post");
		viewPost = new Button("Visualizar Post");
		initBlogText = new TextArea();
		fullBlogText = new TextArea();
		serverResponseLabel = new HTML("");
		
		dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		
		//Configura o painel
		panel.setWidth("600px");
		
		//configura os campos
		initBlogText.setWidth("550px");
		initBlogText.setHeight("200px");
		
		fullBlogText.setWidth("550px");
		fullBlogText.setHeight("200px");
		
		//adiciona os elmentos ao painel
		String htmlHomeBody ="<div class=\"post\"><h1 class=\"title\"><a href=\"#\">" + pageHead +
			"</a></h1><br>&nbsp;<br>";
		panel.add(new HTML(htmlHomeBody));
//		panel.add(initLabel);
		panel.add(initBlogText);
		panel.add(new HTML("<br>&nbsp<br>"));
		panel.add(new HTML("<div class=\"post\"><h1 class=\"title\">" +
				"<a href=\"#\">Informe o conteúdo do seu post" +
				"</a></h1><br>&nbsp;<br>"));
		panel.add(fullBlogText);
		panel.add(new HTML("<br>&nbsp;<br>"));
		
		panel.add(saveEntryButton);
		//panel.add(new HTML("&nbsp;"));
		panel.add(cancelButton);
		panel.add(viewPost);
		panel.add(new HTML("</div>"));
		
		// Add a handler to close the DialogBox
		viewPost.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				RootPanel.get("post").add(new Post("Flebinha Testando", 
						initBlogText.getText(), "O Mestre", fullBlogText.getText()));
			}
		});
		
		MyHandler myHandler = new MyHandler();	
		saveEntryButton.addClickHandler(myHandler);
		
	}
	
	
	public static Widget createNewBlogPage(){
		BlogEntry blog = new BlogEntry();
		
		return blog.getPanel();
	}
	
	public static Widget createNewBlogPage(GreetingServiceAsync service){
		BlogEntry blog = new BlogEntry(service);
		
		return blog.getPanel();
	}
	
	// Create a handler for the sendButton and nameField
	class MyHandler implements ClickHandler, KeyUpHandler {

		/**
		 * Fired when the user clicks on the sendButton.
		 */
		public void onClick(ClickEvent event) {
			//RootPanel.get("content").add(new HTML("OI FUNFOU!!!"));
			savePost();
			//RootPanel.get("content").add(new HTML("OI FUNFOU222!!!"));
			
		}

		/**
		 * Fired when the user types in the nameField.
		 */
		public void onKeyUp(KeyUpEvent event) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				savePost();
			}
		}

		/**
		 * Send the name from the nameField to the server and wait for a response.
		 */
		private void savePost() {
			Post post = new Post("Flebinha Testando", 
					initBlogText.getText(), "O Mestre", fullBlogText.getText());
			String strPost = post.getString();
			postService.addPost(strPost,
					new AsyncCallback<String>() {

						public void onFailure(Throwable caught) {
							// Show the RPC error message to the user
//							dialogBox
//									.setText("Remote Procedure Call - Failure");
//							serverResponseLabel
//									.addStyleName("serverResponseLabelError");
							//serverResponseLabel.setHTML(SERVER_ERROR);
							RootPanel.get("content").add(new HTML("Falhou"));
							//dialogBox.center();
							//closeButton.setFocus(true);
						}

						public void onSuccess(String result) {
							//RootPanel.get("content").add(new HTML("OI FUNFOU!!!"));
							RootPanel.get("content").add(new HTML("<br><br><div " +
									"class=\"post\"><h1 " +
									"class=\"title\"><a href=\"#\"> post adicionado" +
									"</a></h1><br>&nbsp;<br>"));
							
//							dialogBox.center();
							//closeButton.setFocus(true);
						}
					});
		}
	}
	
	
}


